package com.wx.plugin.mp.core;

import com.wx.plugin.util.JSONUtil;
import io.github.elkan1788.mpsdk4j.api.WechatAPI;
import io.github.elkan1788.mpsdk4j.vo.api.FollowList;
import io.github.elkan1788.mpsdk4j.vo.api.Follower;
import io.github.elkan1788.mpsdk4j.vo.api.Follower2;
import io.github.elkan1788.mpsdk4j.vo.api.QRTicket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Collection;
import java.util.List;

/**
 * <p></p>
 *
 * @author  yangz
 * @date    2017/3/2
 * @version latest
 */
@Service
public class WXAPI {
    @Autowired
    private WechatAPI wechatAPI;
    @Autowired
    private WebAuthAPI webAuthAPI;

    public String getOrder(@RequestBody OrderRequestVO orderRequestVO){
        String result = null;
        String accessToken = wechatAPI.getAccessToken();
        for (int i = 0; i < 20; i++) {
            result = webAuthAPI.getOrder(accessToken, orderRequestVO);
            OrderVOResult orderVOResult = JSONUtil.parse(result, OrderVOResult.class);
            OrderVO orderVO = orderVOResult.getOrder();
            if(orderVO != null && orderVO.getBuyer_openid() != null){
                break;
            }else{
                CacheWechatAPIImpl.accessTokenCache.cleanUp();
                continue;
            }
        }
        return result;
    }

    public FollowList getFollowerList(String nextOpenId){
        FollowList result = null;
        for (int i = 0; i < 20; i++) {
            try {
                result = wechatAPI.getFollowerList(nextOpenId);
                break;
            } catch (Exception e) {
                CacheWechatAPIImpl.accessTokenCache.cleanUp();
                continue;
            }
        }
        return result;
    }

    public Follower getFollower(String openId, String lang){
        Follower result = null;
        for (int i = 0; i < 20; i++) {
            try {
                result = wechatAPI.getFollower(openId, lang);
                break;
            } catch (Exception e) {
                CacheWechatAPIImpl.accessTokenCache.cleanUp();
                continue;
            }
        }
        return result;
    }

    public List<Follower> getFollowers(Collection<Follower2> users){
        List<Follower> result = null;
        for (int i = 0; i < 20; i++) {
            try {
                result = wechatAPI.getFollowers(users);
                break;
            } catch (Exception e) {
                CacheWechatAPIImpl.accessTokenCache.cleanUp();
                continue;
            }
        }
        return result;
    }

    public QRTicket createQR(Object sceneId, int expireSeconds){
        QRTicket result = null;
        for (int i = 0; i < 20; i++) {
            try {
                result = wechatAPI.createQR(sceneId, expireSeconds);
                break;
            } catch (Exception e) {
                CacheWechatAPIImpl.accessTokenCache.cleanUp();
                continue;
            }
        }
        return result;
    }
}
